<?php
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class AdminProducts extends Base
{
    // 表名
    private static $table_name = 'kc_infos';
    // 查询字段
    private static $columns = array('products_id', 'products_model', 'products_name', 'products_sales_num', 'products_sale_other', 'products_color', 'products_size', 'products_price', 'products_currency', 'products_description', 'products_evaluate', 'products_add_status', 'products_due_status', 'products_due_date', 'products_add_date');


    public static function getTableName()
    {
        return self::$table_name;
    }

    public static function add($data)
    {
        $db = self::__instance();
        $ret = $db->insert(self::getTableName(), $data);
        return $ret;
    }

    public static function update($id, $data)
    {
        $condition = array("products_id" => $id);
        $db = self::__instance();
        $ret = $db->update(self::getTableName(), $data, $condition);
        return $ret;
    }

    public static function getAllProducts()
    {
        $db = self::__instance();
        $list = $db->select(self::getTableName(), self::$columns);
        return $list;
    }

    public static function getProductById($pid)
    {
        $condition = array("products_id" => $pid);
        $db = self::__instance();
        $sql = "SELECT * FROM kc_infos  WHERE products_id = $pid";
        $product = $db->query($sql)->fetch();
        if ($product) {
            return $product;
        }
        return array();
    }

    public static function getProductByModel($model)
    {
        $db = self::__instance();
        $sql = "SELECT * FROM kc_infos  WHERE products_model = '$model'";
        $product = $db->query($sql)->fetch();
        if ($product) {
            return $product;
        }
        return array();
    }

    public static function getAllBySearch($search, $start = 0, $page_size = 20)
    {
        $db = self::__instance();


        $sql = "SELECT * FROM kc_infos " . $search . " ORDER BY products_id DESC LIMIT $start,$page_size";


        $rets = $db->query($sql)->fetchAll();
        return $rets;
    }


    public static function getAllBySearchNoLimit($search)
    {
        $db = self::__instance();


        $sql = "SELECT * FROM kc_infos " . $search . " ORDER BY products_id DESC";

        $rets = $db->query($sql)->fetchAll();
        return $rets;
    }


    public static function getTenDaysSaleOtherNums()
    {

        $db = self::__instance();
        $sql = "SELECT SUM(products_sale_other) as sum FROM kc_infos WHERE TO_DAYS(CURDATE()) - TO_DAYS(DATE_FORMAT(products_due_date,'%Y-%m-%d')) <=10;";
        $ret = $db->query($sql)->fetch();
        return $ret;
    }


    public static function getCountBySearch($search)
    {
        $db = self::__instance();
        $sql = "SELECT count(*) as c FROM kc_infos $search";
        $ret = $db->query($sql)->fetch();
        return $ret;
    }


    public static function delete($id)
    {
        $condition = array("products_id" => $id);
        $db = self::__instance();
        $ret = $db->delete(self::getTableName(), $condition);
        return $ret;
    }


    public static function getModelForOptions()
    {
        $list = self::getAllModel();

        foreach ($list as $model) {
            $model_options_array [] = $model ['products_model'];
        }

        return $model_options_array;
    }


    public static function getAllModel()
    {
        $db = self::__instance();
        $sql = "SELECT products_model FROM kc_infos";
        $ret = $db->query($sql)->fetchAll();
        return $ret;
    }
}